package br.com.array.android.ma.view;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import br.com.array.android.ma.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Almir Gabriel
 * Date: 26/03/2014
 * Time: 14:13
 */
public class PagesFragment extends LinearLayout {

    private Activity activity;
    private ViewPager viewPager;
    private List<FragmentPage> fragmentPages = new ArrayList<FragmentPage>();
    private FragmentManager fragmentManager;


    public PagesFragment(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PagesFragment(FragmentManager fragmentManager, Activity activity) {
        super(activity);
        this.activity = activity;
        this.fragmentManager = fragmentManager;
    }

    public PagesFragment(FragmentManager fragmentManager, Activity activity, FragmentPage... fragmentPages) {
        super(activity);
        this.activity = activity;
        this.fragmentManager = fragmentManager;
        this.fragmentPages = new ArrayList<FragmentPage>(Arrays.asList(fragmentPages));
        initComponents();
    }

    public PagesFragment(FragmentManager fragmentManager, Activity activity, List<FragmentPage> fragmentPages) {
        super(activity);
        this.activity = activity;
        this.fragmentManager = fragmentManager;
        this.fragmentPages = fragmentPages;
        initComponents();
    }

    private void initComponents() {
        final ViewPager viewPager = (ViewPager) inflate(getContext(), R.layout.view_pager, null).findViewById(R.id.pagerDetalhes);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
                viewPager.setOnPageChangeListener(
                        new ViewPager.SimpleOnPageChangeListener() {
                            @Override
                            public void onPageSelected(int position) {
                                activity.getActionBar().setSelectedNavigationItem(position);
                                viewPager.setCurrentItem(position);
                            }
                        }
                );
            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        viewPager.setAdapter(new PagesAdapter(fragmentManager, fragmentPages));
        addView(viewPager);
        this.viewPager = viewPager;

        montaNavigationTabs(activity.getActionBar(), fragmentPages, viewPager);
    }


    private void montaNavigationTabs(ActionBar actionBar, List<FragmentPage> fragmentPages, final ViewPager viewPager) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

            }
        };

        for (FragmentPage fp : fragmentPages) {
            activity.getActionBar().addTab(
                    activity.getActionBar().newTab()
                            .setText(fp.getTitulo())
                            .setTabListener(tabListener)
            );
        }
    }

    public void setFragmentPages(FragmentPage... fragmentPages) {
        this.fragmentPages = new ArrayList<FragmentPage>(Arrays.asList(fragmentPages));
        initComponents();
    }

    public FragmentPage getFragment(Integer index) {
        return ((PagesAdapter) viewPager.getAdapter()).getFragment(index);
    }

    public Integer getCurrentItem() {
        return viewPager.getCurrentItem();
    }

    public void setCurrentItem(Integer index) {

        viewPager.setCurrentItem(index);
        activity.getActionBar().setSelectedNavigationItem(index);
    }

    public static class PagesAdapter extends FragmentStatePagerAdapter {
        private SparseArray<FragmentPage> mpageReferenceMap = new SparseArray<FragmentPage>();
        private List<FragmentPage> fragmentPages = new ArrayList<FragmentPage>();

        public PagesAdapter(FragmentManager fm, List<FragmentPage> fragmentPages) {
            super(fm);
            if (fragmentPages != null) {
                this.fragmentPages = fragmentPages;
            }
        }

        @Override
        public Fragment getItem(int i) {
            FragmentPage fragmentPage = fragmentPages.get(i);
            mpageReferenceMap.put(i, fragmentPage);
            return fragmentPage;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
            mpageReferenceMap.remove(position);
        }

        public FragmentPage getFragment(int id) {
            return mpageReferenceMap.get(id);
        }

        @Override
        public int getCount() {
            return fragmentPages.size();
        }
    }
}
